package com.casstime.ec.cloud.stastic.transform;

import java.util.List;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import com.casstime.commons.utils.ReflectUtil;
import com.github.pagehelper.Page;

public class CommonTransform {

	public static <F,T> T transform (F from,Class<T> toClass){
		
		if(null == from){
			return null;
		}
		return ReflectUtil.copySameField(from,toClass);
	}
	
	public static <F,T> List<T> transform (List<F> from,Class<T> toClass){
		
		List<T> to = Lists.newArrayList();
		
		
		if(CollectionUtils.isEmpty(from)){
			return to;
		}
		
		for(F f:from){
			to.add(transform(f,toClass));
		}
		
		return to;
		
	}
	
	public static <F,T> Page<T> transform(Page<F> form,Class<T> toClass){
		
		Page<T> to = new Page<T>();
		
		if(CollectionUtils.isEmpty(form)){
			return to;
		}
		
		for(F f:form){
			to.add(transform(f,toClass));
		}
		
		to.setPageNum(form.getPageNum());
		to.setPages(form.getPages());
		to.setPageSize(form.getPageSize());
		to.setStartRow(form.getStartRow());
		to.setEndRow(form.getEndRow());
		to.setTotal(form.getTotal());
		
		return to;
		
	}
}
